package com.hxzy.tms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import com.hxzy.tms.dao.ProjectDao;
import com.hxzy.tms.dao.TaskDao;
import com.hxzy.tms.dao.TeamDao;
import com.hxzy.tms.dao.TeamUserDao;
import com.hxzy.tms.dao.UserDao;
import com.hxzy.tms.dao.UserTaskDao;
import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Task;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.entity.User;
import com.hxzy.tms.entity.UserTask;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TaskService;

import junit.framework.Assert;

public class DefaultTaskService implements TaskService {
	private Team team;
	private TaskDao taskDao;
	private TeamDao teamDao;
	private UserDao userDao;
	private List<Task> tasks;
	private TeamUserDao teamUserDao;
	private UserTaskDao userTaskDao;
	private ProjectDao projectDao;

	public ServiceResult<Task> createTask(String title, String desc, String attachmenturl, Date gmtstart,
			Date gmtfinished, Long projectid, Long teamid, Long uid) {

		Assert.assertNotNull(title);
		Assert.assertNotNull(desc);
		Assert.assertNotNull(attachmenturl);
		Assert.assertNotNull(gmtstart);
		Assert.assertNotNull(gmtfinished);
		Assert.assertNotNull(projectid);
		team = (Team) teamDao.read(Team.class, teamid);
		if (team.getUserCreate().getId() != 1) {
			return new ServiceResult<Task>(-1, null, "您不是该团队的创建者，无权对其发布任务");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("title", title);
		List<Task> listask = taskDao.read("from Task where title=:title", param);
		if (listask.size() >= 1 || !listask.isEmpty()) {

			return new ServiceResult<Task>(-2, null, "检测到相同任务");// 检测到相同用户
		}

		List<User> users = teamUserDao.teamUsers(teamid);
		if (users == null || users.isEmpty()) {
			return new ServiceResult<Task>(-1, null, "团队下还没有成员，请添加成员之后再发布任务");
		}

		Project project = projectDao.read(Project.class, projectid);
		User user = userDao.read(User.class, uid);
		Long now = System.currentTimeMillis() / 1000;
		Long Gmtstart = gmtstart.getTime() / 1000;
		Long Gmtfinished = gmtfinished.getTime() / 1000;
		if (Gmtstart >= Gmtfinished) {
			return new ServiceResult<Task>(-3, null, "任务开始时间不能大于或等于任务结束时间");
		}
        
		Calendar cal = Calendar.getInstance();
		cal.setTime(gmtstart);

		Task task1 = new Task();
		task1.setTitle(title);
		task1.setDesc(desc);
		task1.setGmtStart(Gmtstart);
		task1.setGmtFinished(Gmtfinished);
		task1.setGmtstartyear(cal.get(Calendar.YEAR));
		task1.setGmtstartmonth(cal.get(Calendar.MONTH) + 1);
		task1.setGmtstartdate(cal.get(Calendar.DATE));

		cal.setTime(gmtfinished);
		task1.setGmtFinished(Gmtfinished);
		task1.setGmtfinishedyear(cal.get(Calendar.YEAR));
		task1.setGmtfinishedmonth(cal.get(Calendar.MONTH) + 1);
		task1.setGmtfinisheddate(cal.get(Calendar.DATE));
		task1.setTeam(team);
		task1.setUserCreate(user);
		task1.setGmtModified(now);
		task1.setProject(project);
		task1.setAttachmentUrl(attachmenturl);
		taskDao.create(task1);
		param = new HashMap<String, Object>();

		UserTask userTask = new UserTask();
		userTask.setAttachmentUrl(attachmenturl);
		userTask.setProjectId(projectid);
		userTask.setStatus(0);
		userTask.setUserId(user.getId());
		userTask.setTeamId(teamid);
		userTask.setTaskId(task1.getId());
		userTaskDao.create(userTask);

		return new ServiceResult<Task>(1, null, "发布任务成功");

	}
	
	public ServiceResult<Task> modifyTask(String title, String desc, String attachmenturl, Date gmtstart,
			Date gmtfinished, Long uid, Long teamid, Long taskid) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("uid", uid);
		User user = userDao.read(User.class, uid);
		Team team = teamDao.read(Team.class, teamid);
		if (uid != team.getUserCreate().getId()) {
			return new ServiceResult<Task>(-1, null, "您不是该团队的创建者，无权对该任务修改");

		}
		param = new HashMap<String, Object>();
		param.put("title", title);
		List<Task> listask = taskDao.read("from Task where title=:title", param);
		if (listask.size() <= 0 || listask.isEmpty()) {

			return new ServiceResult<Task>(-2, null, "未有任务可以修改");// 检测到相同用户
		}

		Long now = System.currentTimeMillis() / 1000;
		Long Gmtstart = gmtstart.getTime() / 1000;
		Long Gmtfinished = gmtfinished.getTime() / 1000;
		if (Gmtstart >= Gmtfinished) {
			return new ServiceResult<Task>(-3, null, "任务开始时间不能大于或等于任务结束时间");
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(gmtstart);
		Task task1 = taskDao.read(Task.class, taskid);
		task1.setTitle(title);
		task1.setDesc(desc);
		task1.setUserCreate(user);
		task1.setGmtStart(Gmtstart);
		task1.setGmtstartyear(cal.get(Calendar.YEAR));
		task1.setGmtstartmonth(cal.get(Calendar.MONTH) + 1);
		task1.setGmtstartdate(cal.get(Calendar.DATE));

		cal.setTime(gmtfinished);
		task1.setGmtFinished(Gmtfinished);
		task1.setGmtfinishedyear(cal.get(Calendar.YEAR));
		task1.setGmtfinishedmonth(cal.get(Calendar.MONTH) + 1);
		task1.setGmtfinisheddate(cal.get(Calendar.DATE));

		task1.setGmtModified(now);
		task1.setAttachmentUrl(attachmenturl);
		int result1 = taskDao.merge(task1);

		if (result1 == 1) {
			return new ServiceResult<Task>(1, null, "修改任务成功");
		}

		return new ServiceResult<Task>(-4, null, "修改任务失败");

	}

	public ServiceResult<ArrayList<Task>> mylists(Long uid, Long projectid, Long teamid) {
		Assert.assertNotNull(projectid);
		Assert.assertNotNull(uid);
		Assert.assertNotNull(teamid);

		List<User> users = teamUserDao.teamUsers(teamid);
		int t = 1;
		for (User user1 : users) {
			if (user1.getId() == uid) {
				t = 2;
			}
		}
		if (t != 2) {
			return new ServiceResult<ArrayList<Task>>(-1, null, "您不是该团队的成员无权查看");
		}
		// order by t.gmt_create desc
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("projectid", projectid);
		String sql = "from Task   where project.id =:projectid   ";
		ArrayList<Task> taskss = (ArrayList<Task>) taskDao.read(sql, param);
		if (taskss.size() <= 0 || taskss.isEmpty()) {
			return new ServiceResult<ArrayList<Task>>(-2, null, "当前项目还未发布任何任务");
		}
		Project project = projectDao.read(Project.class, projectid);

		return new ServiceResult<ArrayList<Task>>(1, taskss,
				String.format("项目%s的任务为%d个", project.getTitle(), taskss.size()));
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public TeamDao getTeamDao() {
		return teamDao;
	}

	public void setTeamDao(TeamDao teamDao) {
		this.teamDao = teamDao;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public UserTaskDao getUserTaskDao() {
		return userTaskDao;
	}

	public void setUserTaskDao(UserTaskDao userTaskDao) {
		this.userTaskDao = userTaskDao;
	}

	public TeamUserDao getTeamUserDao() {
		return teamUserDao;
	}

	public void setTeamUserDao(TeamUserDao teamUserDao) {
		this.teamUserDao = teamUserDao;
	}

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

}
